package ar.edu.itba.it.pdc.utils;

import java.io.IOException;

import ar.edu.itba.it.pdc.app.Madonna3;
import ar.edu.itba.it.pdc.app.client.Client;
import ar.edu.itba.it.pdc.protocol.POP3Request;
import ar.edu.itba.it.pdc.protocol.POP3Response;

public class LoggingUtils {

	private static final String CONNECTION_ESTABLISHED = "CONNECTION ESTABLISHED";
	private static final String CONNECTION_FINISHED = "CONNECTION FINISHED";
	private static final String REQUEST = 	"REQUEST ";
	private static final String RESPONSE = 	"RESPONSE";
	private static final String WARNING = "WARN";

	public static void logWelcome(Client client, POP3Response response) throws IOException {
		String resp = response.getResponseString().trim();
		Madonna3.log.info(CONNECTION_ESTABLISHED + " - client: " + client.getHost() + ":" + client.getPort());
		Madonna3.log.info(CONNECTION_ESTABLISHED + " - server says: " + resp);
	}
	
	public static void logGoodbye(Client client) throws IOException {
		Madonna3.log.info(CONNECTION_FINISHED + " - client: " + client.getHost() + ":" + client.getPort());
	}
	
	public static void logSend(Client client, POP3Request request){
		String command = request.getCommand().trim();	
		Madonna3.log.info(REQUEST + " - from: " + client.getHost() + ":" + client.getPort());
		Madonna3.log.info(REQUEST + " - command: '" + command + "'");
	}
	
	public static void logReceive(Client client, POP3Response response) throws IOException {
		String resp = response.getResponseString().trim();
		Madonna3.log.info(RESPONSE + " - to: " + client.getHost() + ":" + client.getPort());
		Madonna3.log.info(RESPONSE + " - response: '" + resp + "'");	
	}
	
	public static void logTooMuchLogins(String username, Client client) throws IOException {
		Madonna3.log.info(WARNING + " - client: " + client.getHost() + ":" + client.getPort());
		Madonna3.log.info(WARNING + " - message: Too much logins as '" + username + "'.");
	}
	
	public static void logInvalidTimeToLogin(String username, Client client) throws IOException {
		Madonna3.log.info(WARNING + " - client: " + client.getHost() + ":" + client.getPort());
		Madonna3.log.info(WARNING + " - message: At this moment, you are not allowed to login as '" + username + "'. Please, try again at a later time.");
	}
	
	public static void logDeleteCondition(Client client, int n) throws IOException {
		Madonna3.log.info(WARNING + " - client: " + client.getHost() + ":" + client.getPort());
		Madonna3.log.info(WARNING + " - message: A delete condition is preventing you from deleting mail " + n + ".");
	}
	
}
